#include "graphicssubclass.h"
#include <QDebug>
#include <QToolTip>
GraphicsSubClass::GraphicsSubClass(QWidget *parent) :
    QGraphicsView(parent)
{
    drawingOn=false;
    whtToDraw=0;
    initContextMenus();
    setDragMode(QGraphicsView::RubberBandDrag);
    setMouseTracking(true);
    setInteractive(true);  
}

void GraphicsSubClass::mousePressEvent(QMouseEvent *event)
{
    if(event->button() == Qt::LeftButton)
    {
        qDebug()<<"left click on view at: "<<event->pos();
        if(whtToDraw==1 && drawingOn)
        {
            if(count==1)
            {

                p0->setX(event->pos().x());
                p0->setY(event->pos().y());
                 QGraphicsView::mousePressEvent(event);
            }
            if(count==2)
            {
                p1->setX(event->pos().x());
                p1->setY(event->pos().y());
                emit getPointForLine(p0,p1);
                drawingOn=false;
                 QGraphicsView::mousePressEvent(event);
            }
            count=count+1;
            if(!drawingOn)
                count=0;
             QGraphicsView::mousePressEvent(event);
        }
        else if(whtToDraw==2 && drawingOn)
        {
            if(count==1)
            {

                p0->setX(event->pos().x());
                p0->setY(event->pos().y());
                 QGraphicsView::mousePressEvent(event);
            }
            if(count==2)
            {
                p1->setX(event->pos().x());
                p1->setY(event->pos().y());
                emit getPointForRect(p0,p1);
                drawingOn=false;
                 QGraphicsView::mousePressEvent(event);
            }
            count=count+1;
            if(!drawingOn)
                count=0;
             QGraphicsView::mousePressEvent(event);
        }
        else if(whtToDraw==3 && drawingOn)
        {
            if(count==1)
            {

                p0->setX(event->pos().x());
                p0->setY(event->pos().y());
                 QGraphicsView::mousePressEvent(event);
            }
            if(count==2)
            {

                p1->setX(event->pos().x());
                p1->setY(event->pos().y());
                 QGraphicsView::mousePressEvent(event);
            }
            if(count==3)
            {

                p2->setX(event->pos().x());
                p2->setY(event->pos().y());
                 QGraphicsView::mousePressEvent(event);
            }
            if(count==4)
            {
                p3->setX(event->pos().x());
                p3->setY(event->pos().y());
                emit getPointForBezier(p0,p1,p2,p3);
                drawingOn=false;
                 QGraphicsView::mousePressEvent(event);
            }
            count=count+1;
            if(!drawingOn)
                count=0;
             QGraphicsView::mousePressEvent(event);
        }
         QGraphicsView::mousePressEvent(event);
    }


}

void GraphicsSubClass::mouseReleaseEvent(QMouseEvent *event)
{
    if (event->modifiers() & Qt::ShiftModifier ) //any other condition
            event->setModifiers(Qt::ControlModifier);

    QGraphicsView::mouseReleaseEvent(event);
}

void GraphicsSubClass::mouseMoveEvent(QMouseEvent *event)
{
    if (event->modifiers() & Qt::ShiftModifier ) //any other condition
            event->setModifiers(Qt::ControlModifier);
    int x=event->pos().x();
    int y=event->pos().y();
    QString msg="("+QString::number(x)+" , "+QString::number(y)+")";
    QToolTip::showText(event->globalPos(),msg);
    QGraphicsView::mouseMoveEvent(event);
}

void GraphicsSubClass::contextMenuEvent(QContextMenuEvent *event)
{
    QMenu myMenu(this);
    myMenu.addAction(clearSelection);
    myMenu.addAction(clearScreen);
    myMenu.exec(event->globalPos());
    QGraphicsView::contextMenuEvent(event);
}

void GraphicsSubClass::initContextMenus()
{
    clearScreen=new QAction(tr("Clear Screen"),this);
    connect(clearScreen,SIGNAL(triggered()),this,SIGNAL(clearMyScreen()));
    clearSelection=new QAction(tr("Clear Selection"),this);
    connect(clearSelection,SIGNAL(triggered()),this,SIGNAL(clearMySelection()));
    qDebug()<<"view class : context menu";
}

void GraphicsSubClass::startDrawing(bool isDraw, int i)
{
    drawingOn=isDraw;
    if(i==1)
    {
        whtToDraw=1;
        count=1;
        p0=new QPoint;
        p1=new QPoint;

    }
    else if(i==2)
    {
        whtToDraw=2;
        count=1;
        p0=new QPoint;
        p1=new QPoint;
    }
    else if(i==3)
    {
        whtToDraw=3;
        count=1;
        p0=new QPoint;
        p1=new QPoint;
        p2=new QPoint;
        p3=new QPoint;
    }
    else if(i==4)
    {
        whtToDraw=4;
    }

}
